/*
 * $Id: frmLoadSave.java 29 2014-03-30 04:41:52Z eldon_r $
 *
 * Created on 25/03/2010, 1:47:51 AM
 */

package customqif;

import java.io.File;
import java.io.FilenameFilter;
import javax.swing.DefaultListModel;

/**
 *
 * @author Edgeberg <eldon_r@users.sf.net>
 */
public class frmLoadSave extends javax.swing.JFrame {
    boolean blnIsSave;
    boolean blnOK = false;
    frmCustomiseQIF parent;

    /** Creates new form frmLoadSave */
    public frmLoadSave(frmCustomiseQIF objParent, boolean blnSave, String strDir) {
        initComponents();
        blnIsSave = blnSave;
        parent = objParent;
        txtNewName.setVisible(blnIsSave);
        // Center this dialog box:
        this.setLocation(parent.getX() + parent.getWidth() - parent.getWidth()/2 - this.getWidth()/2, parent.getY() + parent.getHeight() - parent.getHeight()/2 - this.getWidth()/2);
        loadList(strDir);
        lstPatternFiles.setSelectedValue(parent.strPatternFile, true);
        if (blnIsSave) txtNewName.setText(parent.strPatternFile);
        this.setTitle("Select file to " + (blnSave ? "Save" : "Load"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstPatternFiles = new javax.swing.JList();
        txtNewName = new javax.swing.JTextField();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lstPatternFiles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "1", "2", "3" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstPatternFiles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstPatternFiles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstPatternFilesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstPatternFiles);

        btnOK.setMnemonic('O');
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setMnemonic('C');
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(29, Short.MAX_VALUE)
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtNewName, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNewName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        blnOK = false;
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        blnOK = true;
        if (blnIsSave) {
            parent.strPatternFile = txtNewName.getText();
            parent.saveGrid();
        } else {
            if (lstPatternFiles.getSelectedIndex() >= 0) {
                parent.strPatternFile = lstPatternFiles.getSelectedValue().toString();
                parent.loadGrid();
            }
        }
        this.setVisible(false);
    }//GEN-LAST:event_btnOKActionPerformed

    private void lstPatternFilesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstPatternFilesValueChanged
        if (blnIsSave && (lstPatternFiles.getSelectedIndex() >= 0)) txtNewName.setText(lstPatternFiles.getSelectedValue().toString());
    }//GEN-LAST:event_lstPatternFilesValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstPatternFiles;
    private javax.swing.JTextField txtNewName;
    // End of variables declaration//GEN-END:variables

    private void loadList(String strDir) {
        File myFile = new File(strDir);
        File[] myFileArr = { new File(strDir) };
        String[] strFiles = {""};
        FilenameFilter fnfPatterns = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return (name.endsWith(".Patterns"));
            }
        };
        myFileArr = myFile.listFiles(fnfPatterns);
        strFiles[0]=myFileArr[0].getName().replace(".Patterns", "");
        DefaultListModel listModel = new DefaultListModel();
        for (File f : myFileArr) {
            listModel.addElement((Object) f.getName().replace(".Patterns", ""));
        }
        lstPatternFiles.setModel(listModel);
    }

}